from flask import request
from pymongo import MongoClient
from bson import json_util
import re
from product.mongodb import mongodb

class customService:
    def getData(self,requests):
        reqForm = request.form
        paramsJson = {}
        paramsJson['goods_type_id'] = int(reqForm['goods_type_id'])
        if ('goods_id' in reqForm and '' != reqForm['goods_id']):
            paramsJson['goods_id'] = re.compile(reqForm['goods_id'])
        if ('goods_name' in reqForm and '' != reqForm['goods_name']):
            paramsJson['goods_name'] = re.compile(reqForm['goods_name'])
        if ('sales' in reqForm and '' != reqForm['sales']):
            paramsJson['sales'] = re.compile(reqForm['sales'])
        if ('sign_code' in reqForm and '' != reqForm['sign_code']):
            paramsJson['sign_code'] = re.compile(reqForm['sign_code'])
        page = int(request.form['page'])
        limit = int(request.form['limit'])
        conn = MongoClient(mongodb.ip, 27017)
        db = conn.pinduoduo
        customDB = db.goodsCustom
        data = list(customDB.find(paramsJson).skip((page - 1) * limit).limit(limit))
        count = len(list(customDB.find(paramsJson)))
        returnJson = {
            'code': 0,
            'msg': 'success',
            "count": count,
            "data": (data)
        }
        return json_util.dumps(returnJson)